package com.controller;

import com.entity.GroupUser;
import com.service.GroupUserService;
import com.vo.ResponseVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api")
public class GroupUserController {
    @Autowired
    private GroupUserService groupUserService;
    /**
     * 按照id查询
     * @param id
     * @return
     */
    @GetMapping("/groupUser/getById")
    public ResponseVO getById(Integer id){
        GroupUser result = groupUserService.getById(id);
        return new ResponseVO("200",null,result);
    }


    /**
     * 查询群信息和未读消息数量
     * @param userId
     * @return
     */
    @GetMapping("/groupUser")
    public ResponseVO getGroupUserByUserId(Integer userId){
        List<GroupUser> result = groupUserService.getGroupUserByUserId(userId);
        return new ResponseVO("200",null,result);
    }

    /**
     * 查询某群所有群用户信息
     * @param groupId
     * @return
     */
    @GetMapping("/groupUser/all")
    public ResponseVO getAllByGroupId(Integer groupId){
        List<GroupUser> result = groupUserService.getAllByGroupId(groupId);
        return new ResponseVO("200", null, result);
    }
    /**
     * 添加群成员
     * @param userId      用户ID
     * @param groupInfoId 群ID
     * @return
     */
    @PostMapping("/groupUser/add")
    public ResponseVO addGroupUser(Integer userId, Integer groupInfoId) {
        ResponseVO vo = new ResponseVO();
        Boolean bool = groupUserService.addGroupUser(userId, groupInfoId);
        if (bool) {
            vo.setCode("200");
            vo.setMsg("添加成功！");
        } else {
            vo.setCode("500");
            vo.setMsg("添加失败!");
        }
        return vo;
    }

    /**
     * 修改成员信息
     * 可改动：群内备注、禁言、群角色
     * @param groupUser 成员信息
     * @return
     */
    @PostMapping("/groupUser/edit")
    public ResponseVO editGroupUser(@RequestBody GroupUser groupUser) {
        ResponseVO vo = new ResponseVO();
        Boolean bool = groupUserService.editGroupUser(groupUser);
        if (bool) {
            vo.setCode("200");
            vo.setMsg("修改成功！");
        } else {
            vo.setCode("500");
            vo.setMsg("修改失败！");
        }
        return vo;
    }

    /**
     * 群主转让
     * @param groupUserIdMaster  群主的成员ID
     * @param groupUserIdServant 转让的成员ID
     * @return
     */
    @PostMapping("/groupUser/editRoleTransfer")
    public ResponseVO editRoleTransfer(Integer groupUserIdMaster, Integer groupUserIdServant) {
        ResponseVO vo = new ResponseVO();
        Boolean bool = groupUserService.editRoleTransfer(groupUserIdMaster, groupUserIdServant);
        if (bool) {
            vo.setCode("200");
            vo.setMsg("转让成功！");
        } else {
            vo.setCode("500");
            vo.setMsg("转让失败！");
        }
        return vo;
    }

    /**
     * 根据ID删除
     * @param groupUserId 成员ID
     * @return
     */
    @PostMapping("/groupUser/del")
    public ResponseVO delByIdGroupUser(Integer groupUserId) {
        ResponseVO vo = new ResponseVO();
        Boolean bool = groupUserService.delByIdGroupUser(groupUserId);
        if (bool) {
            vo.setCode("200");
            vo.setMsg("删除成功！");
        } else {
            vo.setCode("500");
            vo.setMsg("删除失败！");
        }
        return vo;
    }

}
